package com.miniapp.core.service.impl;


import com.miniapp.common.utils.DateUtils;
import com.miniapp.common.utils.SecurityUtils;
import com.miniapp.core.domain.PaicFtiRateGen;
import com.miniapp.core.mapper.PaicFtiRateGenMapper;
import com.miniapp.core.service.IPaicFtiRateGenService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 平安货运险费率Service业务层处理
 *
 * @author ywd
 * @date 2024-11-29
 */
@Service
public class PaicFtiRateGenServiceImpl implements IPaicFtiRateGenService
{
    @Autowired
    private PaicFtiRateGenMapper paicFtiRateGenMapper;

    /**
     * 查询平安货运险费率
     *
     * @param id 平安货运险费率主键
     * @return 平安货运险费率
     */
    @Override
    public PaicFtiRateGen selectPaicFtiRateGenById(Long id)
    {
        return paicFtiRateGenMapper.selectPaicFtiRateGenById(id);
    }

    /**
     * 查询平安货运险费率列表
     *
     * @param paicFtiRateGen 平安货运险费率
     * @return 平安货运险费率
     */
    @Override
    public List<PaicFtiRateGen> selectPaicFtiRateGenList(PaicFtiRateGen paicFtiRateGen)
    {
        return paicFtiRateGenMapper.selectPaicFtiRateGenList(paicFtiRateGen);
    }

    /**
     * 新增平安货运险费率
     *
     * @param paicFtiRateGen 平安货运险费率
     * @return 结果
     */
    @Override
    public int insertPaicFtiRateGen(PaicFtiRateGen paicFtiRateGen)
    {

        String productType = paicFtiRateGen.getProductType();
        PaicFtiRateGen paicFtiRateGenQuery = new PaicFtiRateGen();
        paicFtiRateGenQuery.setProductType(productType);
        List<PaicFtiRateGen> paicFtiRateGenList = paicFtiRateGenMapper.selectPaicFtiRateGenList(paicFtiRateGenQuery);
        if(CollectionUtils.isNotEmpty(paicFtiRateGenList)){
            throw new RuntimeException("此产品方案已添加");
        }


        paicFtiRateGen.setCreateTime(DateUtils.getNowDate());
        paicFtiRateGen.setUpdateTime(DateUtils.getNowDate());
        paicFtiRateGen.setUserId(SecurityUtils.getUserId());
        return paicFtiRateGenMapper.insertPaicFtiRateGen(paicFtiRateGen);
    }

    /**
     * 修改平安货运险费率
     *
     * @param paicFtiRateGen 平安货运险费率
     * @return 结果
     */
    @Override
    public int updatePaicFtiRateGen(PaicFtiRateGen paicFtiRateGen)
    {
        Long id = paicFtiRateGen.getId();
        PaicFtiRateGen oldPaicFtiRateGen = paicFtiRateGenMapper.selectPaicFtiRateGenById(id);
        //旧产品方案
        String oldProductType = oldPaicFtiRateGen.getProductType();
        //新产品方案
        String newProductType = paicFtiRateGen.getProductType();


        if(!oldProductType.equals(newProductType)){
            PaicFtiRateGen paicFtiRateGenQuery = new PaicFtiRateGen();
            paicFtiRateGenQuery.setProductType(newProductType);
            List<PaicFtiRateGen> paicFtiRateGenList = paicFtiRateGenMapper.selectPaicFtiRateGenList(paicFtiRateGenQuery);
            if(CollectionUtils.isNotEmpty(paicFtiRateGenList)){
                throw new RuntimeException("此产品方案已添加");
            }

        }



        paicFtiRateGen.setUpdateTime(DateUtils.getNowDate());
        paicFtiRateGen.setUpdateTime(DateUtils.getNowDate());
        paicFtiRateGen.setUserId(SecurityUtils.getUserId());
        return paicFtiRateGenMapper.updatePaicFtiRateGen(paicFtiRateGen);
    }

    /**
     * 批量删除平安货运险费率
     *
     * @param ids 需要删除的平安货运险费率主键
     * @return 结果
     */
    @Override
    public int deletePaicFtiRateGenByIds(Long[] ids)
    {
        return paicFtiRateGenMapper.deletePaicFtiRateGenByIds(ids);
    }

    /**
     * 删除平安货运险费率信息
     *
     * @param id 平安货运险费率主键
     * @return 结果
     */
    @Override
    public int deletePaicFtiRateGenById(Long id)
    {
        return paicFtiRateGenMapper.deletePaicFtiRateGenById(id);
    }
}
